const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');

//获取应用实例
const app = getApp()
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    navUrl: ''
  },
  //页面加载
  onLoad: function(options) {
  },
  //验证登录
  bindGetUserInfo: function(e) {
    let that = this;
    wx.login({
      success: function(res) {
        if (res.code) {
          //登录远程服务器
          util.request(api.AuthLoginByWeixin, {
            code: res.code,
            userInfo: e.detail
          }, 'POST', 'application/json').then(res => {
            if (res.errno === 0) {
              //存储用户信息
              app.globalData.userInfo = res.data.userInfo;
              app.globalData.userId = res.data.userId;
              app.globalData.token = res.data.token;
              app.globalData.openId = res.data.openId;
              wx.setStorageSync('userInfo', res.data.userInfo);
              wx.setStorageSync('token', res.data.token);
              wx.setStorageSync('userId', res.data.userId);
              wx.setStorageSync('openId', res.data.openId);

              //获取自己的店铺列表
              util.request(api.GetMyShops).then(function (res) {
                //计算店铺总数
                let myShopCount = 0;
                let collectedShopCount = 0;

                let myShops = res.data.myShops;
                if (myShops && myShops.length > 0) {
                  //是店主
                  app.globalData.isShopKeeper = true;
                  wx.setStorageSync('isShopKeeper', true);
                  wx.setStorageSync('myShops', myShops);
                  myShopCount += myShops.length;
                }
                else {
                  //不是店主
                  app.globalData.isShopKeeper = false;
                  wx.setStorageSync('isShopKeeper', false);
                }

                //获取所属店铺列表
                util.request(api.GetCollectedShops).then(function (res) {
                  //收藏的店铺列表
                  let collectedShops = res.data.collectedShops;
                  if (collectedShops){
                    collectedShopCount += collectedShops.length;
                  }

                  //当有收藏的店铺或者自己的店铺时
                  if (myShopCount + collectedShopCount > 0) {
                    //只有收藏店铺列表大于1并且没有自己的店铺时
                    if (collectedShopCount == 1 && myShopCount == 0) {
                      wx.setStorageSync('collectedShops', collectedShops);
                      wx.setStorageSync('currentShop', collectedShops[0]);
                      app.globalData.currentRootDeptId = collectedShops[0].rootDeptId;
                      app.globalData.currentUserGrade = collectedShops[0].userGrade;

                      //直接跳转至该店铺首页
                      wx.switchTab({
                        url: '/pages/index/index',
                      })
                    }
                    else {
                      //跳转至店铺列表页
                      wx.setStorageSync('collectedShops', collectedShops);
                      wx.navigateTo({
                        url: '/pages/shopIndex/shopIndex',
                      })
                    }
                  }
                  //没有任何店铺就跳转至邀请码页
                  else {
                    wx.navigateTo({
                      url: '/pages/login/invite/invite'
                    });
                  }
                });

              });
            } 
            else {
              wx.showModal({
                title: '提示',
                content: res.errmsg,
                showCancel: false
              });
            }
          });
        }
      }
    });
  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  }
})